package com.scm.controller;

import com.scm.pojo.Customer;
import com.scm.pojo.SoItem;
import com.scm.pojo.SoMain;
import com.scm.service.*;
import com.scm.util.Page;
import com.scm.util.QueryResult;
import com.scm.util.Result;


import com.scm.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName SellController
 * @Description TODO
 * @Author Xu
 * @Date 2021/7/14 11:45
 * @Version 1.0
 **/
@RestController
public class SellController {

    @Autowired
    private FactoryService factoryService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private SoMainService soMainService;
    @Autowired
    private SoItemService soItemService;

    @Autowired
    private Util util;

    @Autowired
    private ScmService scmService;

    @Autowired
    private ProductService productService;

    @RequestMapping("/getCustomer")
    public Result getCustomer() {
        List<Customer> list = customerService.getCustomer();
        return Result.ok(list);
    }

    @RequestMapping("/addSoMain")
    public Result addSoMain(@RequestBody SoMain soMain, HttpServletRequest request) {
        String account = (String) request.getSession().getAttribute("account");
        soMain.setAccount(account);
        factoryService.addSoMain(soMain);
        return Result.ok(null);
    }

    @RequestMapping("/getAllSoMain")
    public QueryResult getAllSoMain(Integer currentPage, Integer pageSize) {
        Map map = Page.get(currentPage, pageSize);
        List<SoMain> list = soMainService.getPageSoMain(map);
        list = util.showSoMain(list);
        int total = soMainService.count();
        return QueryResult.ok(total, list);
    }

    @RequestMapping("/getSoItem")
    public Result getSoItem(String soID) {
        List<SoItem> soItem = soItemService.getSoItem(soID);
        return Result.ok(soItem);

    }

    @RequestMapping("/updateSoMain")
    public Result getSoMain(String soID) {
        SoMain soMain = soMainService.getSoMain(soID);
        soMain.setItems(soItemService.getSoItem(soID));
        for (SoItem soItem:soMain.getItems()){
            soItem.setProductName(productService.getProductName(soItem.getProductCode()));
        }
        return Result.ok(soMain);
    }

    @RequestMapping("/updateSoMain2")
    public Result updateSoMain2(@RequestBody SoMain soMain) {
        factoryService.updateSoMain(soMain);
        return Result.ok(null);
    }
    @RequestMapping("/selectSoMain")
    public QueryResult selectSoMain(Integer currentPage, Integer pageSize,String date,String soID,String status) {
        Map map = Page.get(currentPage,pageSize);
        map.put("soID",soID);
        map.put("status",status);
        if (!date.isEmpty()){
           String[] time = date.split("~");
           map.put("minDate",time[0].trim()+" 00:00");
           map.put("maxDate",time[1].trim()+" 00:00");
       }
        int total = soMainService.getCountLikeSoMain(map);
        List list = soMainService.getLikeSoMain(map);
        list=util.showSoMain(list);
        return QueryResult.ok(total,list);
    }

    @RequestMapping("endSoMain")
    public QueryResult endSoMain1(Integer currentPage, Integer pageSize,String payType){
        Map<String, Object> map = Page.get(currentPage, pageSize);
        map.put("payType",payType);
        if (payType.equals("款到发货")){
            map.put("status","3");
        }else {
            map.put("status","4");
        }
        List<SoMain> list = soMainService.endSoMain(map);
        list=util.showSoMain(list);
        int total= soMainService.endCountSoMain(map);
        return QueryResult.ok(total,list);
    }
    @RequestMapping("realEndSoMain")
    public Result realEndSoMain(String soID){
        soMainService.realEndSoMain(soID);
        return Result.ok(null);
    }
}